﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using System.Collections;
using ECCIT;
using ECCIT.Web;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using System.Data;
using System.IO;
using OgilvyOne.UKNow.Configuration;

public partial class admin_UKNOW_news_new_add : AdminBasePage
{
    private const string VIEWSTATE_COLUMN_ID = "columnid";
    private const string VIEWSTATE_LAYER_ID = "layerid";
    private const string VIEWSTATE_NEWS_ID = "sNewsID";
    private const string VIEWSTATE_IMG_LIST = "img_list";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState[VIEWSTATE_COLUMN_ID] = int.Parse(Request.Params[VIEWSTATE_COLUMN_ID]);
            ViewState[VIEWSTATE_LAYER_ID] = Request.Params[VIEWSTATE_LAYER_ID];

            string sNewsID = RequestContext.GetParamValueByParamName(VIEWSTATE_NEWS_ID, "");
            ViewState[VIEWSTATE_NEWS_ID] = sNewsID;
            ViewState[VIEWSTATE_IMG_LIST] = "";


            if (string.IsNullOrEmpty(sNewsID))
                InitPage(null, 0);
            else
                InitPage(int.Parse(sNewsID), 0);
        }

        //Response.Write(LangID);
    }
    private void InitPage(int? sNewsID, int type)
    {
        InitControls();

        NewsModel model = null;
        if (sNewsID.HasValue)
        {
            NewsBLL bll = new NewsBLL(this.LangID);
            model = bll.selNew(sNewsID.Value);
        }

        BindEventsToControls(model, type);
    }

    private void BindEventsToControls(NewsModel info, int type)
    {

        if (ViewState[VIEWSTATE_NEWS_ID].ToString() == "")
        {
            infoname.Text = "新增新闻";

            if (info == null)
            { return; }
        }
        else
        {
        infoname.Text = "修改新闻";
        }
        title.Text = info.Title;
        shortDescript_txt.Text = info.ShortDescription;
        txt_phone.Text = info.PhoneContent;
        //templetID    listPicFile
        content.Text = info.Content;
        //将图片路径保存在ViewState里面，用于在没有修改图片的情况下保存数据的时候从ViewState里面将原来的图片路径取出来保存到数据库
        ViewState[VIEWSTATE_IMG_LIST] = info.ImageName;
        hot_new.SelectedValue = info.IsHotspot? "1" : "0";
        
        pubState.SelectedValue = info.State.ToString();
        txt_order.Text = info.Order.ToString();
        hf_createtime.Value = info.CreateTime;
        //发布时间，用于前台显示，2012-8-16
        txt_publishtime.Text = info.UpdatedTime.ToString();
        templateId.SelectedValue = (info.TemplateID.HasValue) ? info.TemplateID.Value.ToString() : "";                               
    }

    private void InitControls()
    {
        
        //初始新闻模板
        this.templateId.Items.Clear();
        ModelManageBLL mmb = new ModelManageBLL();
        DataSet temdata = mmb.GetModel("");
        if (temdata.Tables.Count > 0 && temdata.Tables[0].Rows.Count > 0)
        {
            templateId.DataSource = temdata.Tables[0];
            templateId.DataTextField = "tem_name";
            templateId.DataValueField = "tem_id";
            templateId.DataBind();
        }
    }
    protected void Sub_click(object sender, EventArgs e)
    {
        string sNewsID = Request.QueryString["sNewsID"];
        //string sNewsID = (string)ViewState[VIEWSTATE_IMG_LIST];    // 获取新闻ID

        NewsBLL newsbll = new NewsBLL(LangID);
        NewsModel info = null;

        if (!string.IsNullOrEmpty(sNewsID))
            info = newsbll.selNew(int.Parse(sNewsID));           // 如果有新闻ID，取回新闻model

        string uploadedImageName = null;

        if (listPicFile.PostedFile.ContentLength == 0)
        {
            uploadedImageName = (info == null) ? "" : info.ImagePath;  // 如果用户没有上传图片，且为编辑图片，则图片名称用原来的
        }
        else
        {
            ResultInfo result = UploadedFile.Save(listPicFile, ConfigCacheManager.FilePath.UploadImagePath, UploadedFile.RenameHandlers.RenameToGuid);
            uploadedImageName = (result.Result) ? ((FileInfo)result.ReturnObject).Name : null;
        }

        if (info == null)
            info = new NewsModel();
        info.ID = null;
        if (!string.IsNullOrEmpty(ViewState[VIEWSTATE_NEWS_ID].ToString()))
        {
            info.ID = int.Parse(ViewState[VIEWSTATE_NEWS_ID].ToString());
        }
            info.Title = title.Text;
            info.ShortDescription = shortDescript_txt.Text;
            info.LanguageID = LangID;
            info.Content = content.Text;

            int iorder;
            int.TryParse(txt_order.Text, out iorder);
            info.Order = iorder;
            info.InsertedTime = !string.IsNullOrEmpty(sNewsID) ? info.InsertedTime : DateTime.Now;

            //更新发布时间 2012-8-16
            DateTime publishtime;
            info.UpdatedTime = DateTime.TryParse(txt_publishtime.Text, out publishtime) ? publishtime : DateTime.Now;

            info.IsHotspot = hot_new.SelectedItem.Selected;
            info.State = int.Parse(pubState.SelectedValue);
            info.ImageName = string.IsNullOrEmpty(uploadedImageName) ? ViewState[VIEWSTATE_IMG_LIST].ToString() : uploadedImageName;
            info.ColumnID = Convert.ToInt32(Request.QueryString["columnid"]);//cp
            info.TemplateID = Convert.ToInt32(this.templateId.SelectedValue);//cp
            info.PhoneContent = txt_phone.Text;
            ResultInfo res = newsbll.Save(info);

        if (res.Result)
        {
            Response.Write("<script> alert('保存成功');window.close(); </script>");// 
        }
        else
        {
            Response.Write(res.Message);
            Response.Write("<script>alert('保存失败');</script>");
            
        }

    }

}